<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>权限列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="myForm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">权限名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">权限地址</label>
                            <div class="layui-input-inline">
                                <input type="text" name="url" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">所属菜单</label>
                            <div class="layui-input-inline">
                                <input type="text" autocomplete="off" class="layui-input" id="title">
                                <input type="hidden" name="pid" placeholder="请输入" autocomplete="off" class="layui-input" id="pid">
                                <!--<select name="pid" id="pid">
                                    <option value="">请选择</option>
                                </select>-->
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add">新增权限</button>
            </div>
        </script>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs data-count-edit layui-btn-danger" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../js/my-utils.js" charset="utf-8"></script>
<script src="../../js/layutil.js" charset="utf-8"></script>
<script>
    var selected = [];//多选
    var checkList = {};//全部
    var checked = [];//编辑(单选)
    layui.use(['form', 'table', 'laydate', 'tableSelect'], function () {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            tableSelect = layui.tableSelect;
        //获取全部的菜单
        var list;
        layutil.post("/myerp/menu/all",null,function (res) {
            list = res.data;
        });

        var pidname = function (d) {
            for (var o =0;o<list.length;o++) {
                if (d.pid == list[o]["id"]) {
                    return "<span>" + list[o]["title"] + "</span>"
                }
            }
        };

        tableSelect.render({
            elem: '#title',
            checkedKey: 'id',
            searchType: 'more', //开启多条件搜索
            searchList: [
                {searchKey: 'id', searchPlaceholder: '编号搜索'},
                {searchKey: 'title', searchPlaceholder: '标题搜索'},
            ],
            table: {
                url: '/myerp/menu/table',
                type: "GET",
                dataType:"json",
                headers:{'Authorization':'Bearer '+ JSON.parse(localStorage.getItem('userdetail')).token},
                cols: [[
                    { type: 'radio' },
                    { field: 'id', title: '编号' },
                    { field: 'title', title: '标题' }
                ]]
            },
            done: function (elem, data) {
                console.log(data);
                $("#pid").val(data.data[0].id);
                $("#title").val(data.data[0].title);
                /*var NEWJSON = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.username)
                })
                elem.val(NEWJSON.join(","))*/
            }
        });

        table.render({
            elem: '#currentTableId',
            url: '/myerp/permission/list',
            toolbar: '#toolbarDemo',
            headers:{'Accept': 'application/json','Authorization':'Bearer '+ JSON.parse(localStorage.getItem('userdetail')).token},
            cols: [[
                {field: 'name', width: 200, title: '权限名称'},
                {field: 'url', width: 350, title: '权限地址'},
                {field: 'pid', width: 200, title: '所属上级菜单',templet:pidname},
                {field: 'id', width: 10, title: '唯一编号',hide:true},
                {field: 'description', width: 200, title: '描述'},
                {title: '操作', minWidth: 400, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            parseData: function(res){
                if(res.code == 401) window.location.href = '/page/login-1.html';
                checkList = res.data;
            },
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');
            return false;
        });
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '新增权限',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/permission/permission-add.html',
                    end: function () {//无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
                        table.reload('currentTableId');
                    }
                });
                // $(window).on("resize", function () {
                //     layer.full(index);
                // });
            }
        });
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent=== 'edit') {
                var index = layer.open({
                    title: '编辑权限',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/permission/permission-edit.html',
                    success: function() {
                        var body = layer.getChildFrame('body', index);
                        for(var key in data) {
                            body.find("input[name='"+key+"']").val(data[key]);
                        }

                    },
                    end: function () {//无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
                        table.reload('currentTableId');
                    }
                });
                // $(window).on("resize", function () {
                //     layer.full(index);
                // });
            }else if(obj.event === 'delete') {
                var str = "确定删除权限信息？";
                layer.confirm(str, {btn: ['确定', '取消'], title: "提示"}, function (index) {
                    $.ajax({
                        url: "/myerp/permission/delete",
                        type: "POST",
                        dataType:"json",
                        headers:{'Authorization':'Bearer '+ JSON.parse(localStorage.getItem('userdetail')).token},
                        data:data,
                        success: function(res) {
                            console.log('res', res);
                            res = res.body;
                            if(res.code == '0') {
                                layutil.success("删除成功",1,function () {
                                    table.reload('currentTableId');
                                });
                                return false;
                            }
                            layutil.fail("删除失败",1,function () {
                                table.reload('currentTableId');
                            });
                        },
                        error:function (data) {
                            layutil.fail("删除失败",1,function () {
                                table.reload('currentTableId');
                            });
                        }
                    });
                })
            }
        });


    });
</script>
    </body>
</html>